<?php
namespace app\passenger\model;

use app\BaseModel;
use think\facade\Db;

class Log extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_passenger_user_log';




    protected $append =[




    ];


    /**
     * @param $dis
     * @param $page
     * @功能说明:优惠券记录表
     * @author chenniang
     * @DataTime: 2020-03-09 18:42
     */

    public function AdminList($dis,$mapor,$page=10){

        $data = $this->alias('a')
                ->join('longbing_card_user b','a.user_id = b.id')
                ->join('longbing_card_user_info c','a.user_id = c.fans_id','left')
                ->where($dis)
                ->where(function ($query) use ($mapor){
                    $query->whereOr($mapor);
                })
                ->field('a.*,b.nickName,c.name,sum(a.pv) as pv,sum(a.get_num) as get_num,sum(a.use_num) as use_num,sum(a.share_num) as share_num')
                ->group('a.user_id')
                ->order('a.id desc')
                ->paginate($page)
                ->toArray();

        return $data;
    }


    /**
     * User: chenniang
     * Date: 2019-12-18 14:11
     * @return void
     * descrption:记录详情
     */
    public function logInfo($dis){

        $data = $this->where($dis)->find();

        if(empty($data)){

            $this->logAdd($dis);

            $data = $this->where($dis)->find();
        }

        return !empty($data)?$data->toArray():[];
    }

    /**
     * User: chenniang
     * Date: 2019-12-18 14:11
     * @return void
     * descrption:添加记录
     */
    public function logAdd($data){

        $today = date('Y-m-d');
        //浏览量
        $data['pv']              = 0;
        //分享数
        $data['share_num']       = 0;
        //领券数
        $data['get_num']         = 0;
        //使用数量
        $data['use_num']         = 0;
        //创建时间
        $data['create_time']     = time();
        //更新时间
        $data['update_time']     = time();
        //今天浏览
        $data['pv_today']        = $today;
        //今天浏览数
        $data['pv_today_num']    = 0;
        //今日分享
        $data['share_today']     = $today;
        //今日分享数
        $data['share_today_num'] = 0;

        $res = $this->insert($data);

        return $res;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-03-23 17:31
     * @功能说明:加数量
     */
    public function logInset($dis,$filed,$num=1){

        $data = $this->logInfo($dis);

        $res  = $this->logUpdate($dis,["$filed"=>Db::raw("$filed+$num")]);

        return $res;

    }

    /**
     * User: chenniang
     * Date: 2019-12-18 14:13
     * @return void
     * descrption:编辑记录
     */
    public function logUpdate($dis,$data){

        //创建时间
        $data['create_time']     = time();
        //更新时间
        $data['update_time']     = time();

        $res = $this->where($dis)->update($data);

        return $res;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-03-23 15:33
     * @功能说明:分享列表
     */
    public function staffShareList($staff_id,$active_id){


        $dis = [
            //员工id
            'a.staff_id' => $staff_id,
            //活动id
            'a.active_id'=> $active_id
        ];
        $data = $this->alias('a')
            ->join('longbing_card_user_info b','a.user_id = b.fans_id','left')
            ->field(['a.*','b.name as user_name','b.avatar as user_img','a.user_id'])
            ->where($dis)
            ->order('id desc')
            ->paginate(10)
            ->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){
                //姓名
                $v['user_name'] = !empty($v['user_name'])?$v['user_name']:Db::name('longbing_card_user')->where(['id'=>$v['user_id']])->value('nickName');
                //头像
                $v['user_img']  = !empty($v['user_img'])?$v['user_img']:$this->UserInfoAratar($v['user_id']);
                //友好时间
                $f_time = datachange($v,'update_time');

                $v['update_time'] = $f_time['radar_group'].$f_time['radar_time'];
            }
        }
        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-03-23 17:02
     * @功能说明:初始化
     */
    public function initToday(){

        //今天
        $to_day = date('Y-m-d',time());

        $dis_log[] = ['pv_today','<>',$to_day];

        $this->where($dis_log)->update(['pv_today'=>$to_day,'pv_today_num'=>0]);

        $dis_logs[] = ['share_today','<>',$to_day];

        $this->where($dis_logs)->update(['share_today'=>$to_day,'share_today_num'=>0]);

        return true;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-03-24 09:57
     * @功能说明:员工端端详情
     */
    public function staffUserInfo($user_id,$active_id,$staff_id){

        $dis = [
            //员工id
            'a.staff_id' => $staff_id,
            //活动id
            'a.active_id'=> $active_id,
            //用户id
            'a.user_id'  => $user_id
        ];

        $data = $this->alias('a')
                ->join('longbing_card_user_info b','a.user_id = b.fans_id','left')
                ->field(['a.*','b.name as user_name','b.avatar as user_img'])
                ->where($dis)
                ->find();

        if(!empty($data)){

            $data = $data->toArray();
            //姓名
            $data['user_name'] = !empty($data['user_name'])?$data['user_name']:Db::name('longbing_card_user')->where(['id'=>$data['user_id']])->value('nickName');
            //头像
            $data['user_img']  = !empty($data['user_img'])?$data['user_img']:$this->UserInfoAratar($data['user_id']);

        }
        //友好时间
        $f_time = datachange($data,'update_time');

        $data['update_time'] = $f_time['radar_group'].$f_time['radar_time'];

        //活动模型
        $active_model = new IndexActive();
        //活动新人券
        $data['coupon_name'] = $active_model->where(['id'=>$active_id])->value('user_coupon_data');
        return $data;

    }


    public function UserInfoAratar($uid){
        $data = Db::name('longbing_card_user')->where(['id'=>$uid])->value('avatarUrl');
        $data = !empty($data)?$data:'https://lbqnyv2.migugu.com/defaultAvatar.png';
        return $data;
    }












}